package artid.com.arttogoa;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import artid.com.arttogoa.R;

import artid.com.arttogoa.util.BitmapUtil;
import artid.com.arttogoa.view.ClipImageLayout;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 *    
 * 项目名称：Courier   
 * 类名称：Clip_Activity   
 * 类描述：自定义图片剪切页面  
 * 创建人：耿卫斌   
 * 创建时间：2015-9-11 下午4:08:18
 */
public class Clip_Activity extends Activity implements OnClickListener{
	private Context mContext;
	private ImageButton ShutDownBtn,subMitBtn;
	private ClipImageLayout mClipImageLayout;
	private String ImagePath;//图片路径

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//获取拍照后和相册选取之后图片路径
		Intent i=getIntent();
		ImagePath=i.getStringExtra("ImagePath");
		//生成剪裁后的图片
		Bitmap mBitmap= BitmapUtil.getSmallBitmap(ImagePath, 500, 500);
		if(mBitmap!=null){
			int d=BitmapUtil.getBitmapDegree(ImagePath);
			ClipImageLayout.setBitmap(BitmapUtil.rotateBitmapByDegree(mBitmap, d));
		}else{
			finish();
		}
		setContentView(R.layout.activity_clip);
		initView();
	}

	private void initView(){
		mContext=Clip_Activity.this;
		ImageButton cancle=(ImageButton) findViewById(R.id.Clip_ShutDown_Btn);
		cancle.setOnClickListener(this);
		TextView subMit=(TextView) findViewById(R.id.Clip_sunMit_Btn);
		subMit.setOnClickListener(this);
		mClipImageLayout=(ClipImageLayout) findViewById(R.id.Clip_ClipImageLayout);
	}
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.Clip_ShutDown_Btn:
			//退出
			finish();
			break;
		case R.id.Clip_sunMit_Btn:
			//提交
			Bitmap bitmap = mClipImageLayout.clip();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
			byte[] datas = baos.toByteArray();
			if(datas!=null){
				Bitmap mBitmap=byteToBitmap(datas);
				try {
					//剪切后的图片地址
					Uri url = Uri.parse(saveBitmapToFile(mBitmap));
					String picPath = url.toString();//剪切后的图片地址
					Intent intent = new Intent("com.clip.action");  
					intent.putExtra("PhotoPath", picPath);  
					setResult(200, intent);
					finish();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				Toast.makeText(mContext, "剪切图片失败!!", 1).show();
			}
			break;

		default:
			break;
		}
	}


	// 将流转成Bitmap对象
	private Bitmap byteToBitmap(byte[] data) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length, options);
		int i = 0;
		while (true) {
			if ((options.outWidth >> i <= 1000)
					&& (options.outHeight >> i <= 1000)) {
				options.inSampleSize = (int) Math.pow(2.0D, i);
				options.inJustDecodeBounds = false;
				b = BitmapFactory
						.decodeByteArray(data, 0, data.length, options);
				break;
			}
			i += 1;
		}
		return b;
	}

	public static String getFilePath() {
		File path = Environment.getExternalStorageDirectory();
		return path.toString();

	}

	/**
	 * Save Bitmap to a file.保存图片到SD卡。
	 */
	public static String saveBitmapToFile(Bitmap bitmap) throws IOException {
		BufferedOutputStream os = null;
		String _file = BitmapUtil.getFilePath();
		try {
			File file = new File(_file);
			int end = _file.lastIndexOf(File.separator);
			String _filePath = _file.substring(0, end);
			File filePath = new File(_filePath);
			if (!filePath.exists()) {
				filePath.mkdirs();
			}
			file.createNewFile();
			os = new BufferedOutputStream(new FileOutputStream(file));
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.fillInStackTrace();
				}
			}
		}
		return _file;
	}

}
